<?php

class Action {

    public static function getReferer() {
        return $_SERVER['HTTP_REFERER'];
    }
    
    public static function requestUrl() {
        global $CAST_BUNDLE;
        return $CAST_BUNDLE['URI'];
    }

    public static function enableLayout() {
        global $CAST_BUNDLE;
        $CAST_BUNDLE['useLayout'] = true;
    }
    
    public static function disableLayout() {
        global $CAST_BUNDLE;
        $CAST_BUNDLE['useLayout'] = false;
    }

    public static function enableView() {
        global $CAST_BUNDLE;
        $CAST_BUNDLE['useView'] = true;
    }

    public static function disableView() {
        global $CAST_BUNDLE;
        $CAST_BUNDLE['useView'] = false;
    }

    public static function layout($name) {
        global $CAST_BUNDLE;
        $CAST_BUNDLE['layout'] = $name;
    }

    public static function view($name) {
        global $CAST_BUNDLE;
        $CAST_BUNDLE['view'] = $name;
    }

    public static function setConfig($name, $value) {
        global $CAST_CONFIG;
        $CAST_CONFIG[$name] = $value;
    }

    public static function hasPost() {
        return !empty($_POST);
    }

    public static function getPost($key) {
        return $_POST[$key];
    }

    public static function getAllPost() {
        return $_POST;
    }

    public static function hasGet() {
        return !empty($_GET);
    }

    public static function getGet($key) {
        return $_GET[$key];
    }

    public static function getAllGet() {
        return $_GET;
    }

    public static function hasParam() {
        global $CAST_PARAMS;
        return !empty($CAST_PARAMS);
    }

    public static function getParam($index) {
        global $CAST_PARAMS;
        return $CAST_PARAMS[$index];
    }

    public static function getAllParam() {
        global $CAST_PARAMS;
        return $CAST_PARAMS;
    }

    public static function redirect($actionname, $actionparams = array(), $getparams = null) {
        global $CAST_CONFIG;
        $url = '/'.$CAST_CONFIG['applicationRoot'].$actionname.'/'.implode('/', $actionparams);
        if (!empty($getparams)) {
            if (is_string($getparams))
            $url.= "?$getparams";
            else if (is_array($getparams)) {
                $url .= '?';
                foreach($getparams as $key => $value) {
                    $url .= "$key=$value&";
                }
                $url = substr_replace($url, '', strlen($url)-1, 1);
            }
        }
        //echo "url is $url.";
        header("Location: $url");
    }

    public static function exec($name) {
        global $CAST_BUNDLE;
        $path = "{$CAST_BUNDLE['dir']}/actions/$name.php";
        if (file_exists($path))
        require $path;
        //todo unset more
        unset ($CAST_BUNDLE);
//        print_r(get_defined_vars());
        return get_defined_vars();
    }

    public static function hasFile($name) {
        return array_key_exists($name, $_FILES);
    }

    public static function getFile($name) {
        $files = array();
        foreach ($_FILES[$name] as $key => $file) {
            foreach($file as $id=>$value) {
                $files[$id][$key] = $value;
            }
        }
        return $files;
    }

    public static function moveFile($name, $location) {
        move_uploaded_file($name, $location);
    }

    public static function go($url) {
        header("Location: $url");
    }
}
?>